<template>
  <div class="auth-page">
    <div class="back-home">
      <el-button type="text" @click="goHome" class="back-btn">
        <el-icon><ArrowLeft /></el-icon> 返回首页
      </el-button>
    </div>
    
    <div class="auth-container">
      <div class="auth-card">
        <div class="auth-header">
          <div class="logo-container">
            <img src="../../assets/logo.png" alt="自驾游探索" class="auth-logo" />
          </div>
          <h1>创建新账号</h1>
          <p>加入我们，开启您的自驾探索之旅</p>
        </div>
        
        <el-form 
          ref="registerFormRef" 
          :model="registerForm" 
          :rules="registerRules" 
          class="auth-form"
          @submit.prevent="handleRegister"
        >
          <el-form-item prop="username">
            <el-input 
              v-model="registerForm.username" 
              placeholder="用户名"
              :prefix-icon="User"
              size="large"
              class="custom-input"
            />
          </el-form-item>
          
          <el-form-item prop="email">
            <el-input 
              v-model="registerForm.email" 
              placeholder="邮箱地址"
              :prefix-icon="Message"
              size="large"
              class="custom-input"
            />
          </el-form-item>
          
          <el-form-item prop="phone">
            <el-input 
              v-model="registerForm.phone" 
              placeholder="手机号码"
              :prefix-icon="Phone"
              size="large"
              class="custom-input"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input 
              v-model="registerForm.password" 
              type="password" 
              placeholder="密码"
              :prefix-icon="Lock"
              show-password
              size="large"
              class="custom-input"
            />
          </el-form-item>
          
          <el-form-item prop="confirmPassword">
            <el-input 
              v-model="registerForm.confirmPassword" 
              type="password" 
              placeholder="确认密码"
              :prefix-icon="Lock"
              show-password
              size="large"
              class="custom-input"
            />
          </el-form-item>
          
          <div class="form-agreement">
            <el-checkbox v-model="registerForm.agreeTerms">
              我已阅读并同意
              <el-button type="text" class="agreement-link" @click="showTerms">服务条款</el-button>
              和
              <el-button type="text" class="agreement-link" @click="showPrivacy">隐私政策</el-button>
            </el-checkbox>
          </div>
          
          <el-button 
            type="primary" 
            class="submit-btn" 
            :loading="userStore.loading"
            :disabled="!registerForm.agreeTerms"
            @click="handleRegister"
            size="large"
          >
            注册
          </el-button>
        </el-form>
        
        <div class="auth-footer">
          <p>已有账号? <el-button type="text" @click="goToLogin" class="login-link">立即登录</el-button></p>
        </div>
      </div>
      
      <div class="auth-background">
        <div class="bg-pattern"></div>
        <div class="bg-quote">
          <h2>"每一次旅行都是一次成长，<br>每一段路程都是一次发现。"</h2>
          <p>— 自驾游探索</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, nextTick } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { User, Lock, Message, ArrowLeft, Phone } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { useUserStore } from '@/stores/user';

const router = useRouter();
const route = useRoute();
const userStore = useUserStore();
const registerFormRef = ref(null);
const loading = ref(false);

// 表单数据
const registerForm = reactive({
  username: '',
  email: '',
  phone: '',
  password: '',
  confirmPassword: '',
  agreeTerms: false
});

// 表单验证规则
const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应在3-20个字符之间', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号码', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入有效的手机号码', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 8, message: '密码长度至少为8个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入的密码不一致'));
        } else {
          callback();
        }
      },
      trigger: 'blur'
    }
  ],
  agreeTerms: [
    {
      validator: (rule, value, callback) => {
        if (!value) {
          callback(new Error('请同意用户协议和隐私政策'));
        } else {
          callback();
        }
      },
      trigger: 'change'
    }
  ]
};

// 在组件挂载时，确保表单是空的
onMounted(() => {
  // 清空表单数据，确保不会自动填充之前登录用户的信息
  registerForm.username = '';
  registerForm.email = '';
  registerForm.phone = '';
  registerForm.password = '';
  registerForm.confirmPassword = '';
  registerForm.agreeTerms = false;
  
  // 如果有必要，也可以清除相关的本地存储
  // localStorage.removeItem('rememberedUsername');
});

const handleRegister = async () => {
  if (!registerFormRef.value) return;
  
  await registerFormRef.value.validate(async (valid) => {
    if (valid) {
      if (!registerForm.agreeTerms) {
        ElMessage.warning('请阅读并同意服务条款和隐私政策');
        return;
      }
      
      try {
        loading.value = true;
        const response = await userStore.register({
          username: registerForm.username,
          email: registerForm.email,
          phone: registerForm.phone,
          password: registerForm.password,
          confirmPassword: registerForm.confirmPassword
        });
        
        if (response) {
          ElMessage.success('注册成功，请登录');
          router.push('/login');
        }
      } catch (error) {
        console.error('注册失败:', error);
      } finally {
        loading.value = false;
      }
    } else {
      return false;
    }
  });
};

const goToLogin = () => {
  router.push('/login');
};

const showTerms = () => {
  ElMessageBox.alert(
    '这里是服务条款的详细内容...',
    '服务条款',
    {
      confirmButtonText: '我已阅读并同意'
    }
  );
};

const showPrivacy = () => {
  ElMessageBox.alert(
    '这里是隐私政策的详细内容...',
    '隐私政策',
    {
      confirmButtonText: '我已阅读并同意'
    }
  );
};

const goHome = () => {
  router.push('/');
};
</script>

<style scoped>
@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css');

.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fa;
  padding: 20px;
}

.auth-container {
  display: flex;
  width: 100%;
  max-width: 1000px;
  min-height: 600px;
  background-color: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.auth-card {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
}

.auth-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo-container {
  margin-bottom: 20px;
}

.auth-logo {
  height: 60px;
}

.auth-header h1 {
  font-size: 28px;
  color: #303133;
  margin-bottom: 10px;
  font-weight: 600;
}

.auth-header p {
  color: #606266;
  font-size: 16px;
}

.auth-form {
  margin-bottom: 20px;
}

.custom-input {
  --el-input-height: 50px;
  --el-input-border-radius: 8px;
}

.form-agreement {
  margin-bottom: 20px;
}

.agreement-link {
  color: #409EFF;
  padding: 0 2px;
}

.submit-btn {
  width: 100%;
  height: 50px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 20px;
  background: linear-gradient(135deg, #409EFF 0%, #007BFF 100%);
  border: none;
}

.auth-footer {
  margin-top: auto;
  text-align: center;
  color: #606266;
}

.login-link {
  color: #409EFF;
  font-weight: 500;
}

.auth-background {
  flex: 1;
  background: linear-gradient(135deg, #3498db 0%, #1abc9c 100%);
  position: relative;
  display: none;
  overflow: hidden;
}

.bg-pattern {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url('');
  opacity: 0.2;
}

.bg-quote {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: white;
  text-align: center;
  width: 80%;
}

.bg-quote h2 {
  font-size: 24px;
  font-weight: 300;
  line-height: 1.5;
  margin-bottom: 20px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.bg-quote p {
  font-size: 16px;
  opacity: 0.8;
}

@media (min-width: 768px) {
  .auth-background {
    display: block;
  }
}

@media (max-width: 767px) {
  .auth-container {
    flex-direction: column;
  }
  
  .auth-card {
    padding: 30px 20px;
  }
}

.back-home {
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 10;
}

.back-btn {
  display: flex;
  align-items: center;
  font-size: 16px;
  color: #606266;
  transition: all 0.3s;
}

.back-btn:hover {
  color: #409EFF;
  transform: translateX(-5px);
}

.back-btn .el-icon {
  margin-right: 5px;
}

@media (max-width: 767px) {
  .back-home {
    top: 10px;
    left: 10px;
  }
  
  .back-btn {
    font-size: 14px;
  }
}
</style> 